package ps.client.plugin.eq2.gui.dialog.curses;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ps.client.plugin.eq2.gui.dialog.curses.timer.Timer;
import ps.client.plugin.eq2.gui.dialog.curses.timer.TimerEvent;
import ps.client.plugin.eq2.gui.dialog.curses.timer.TimerListener;

@SuppressWarnings("serial")
public class TimerPanel extends JPanel implements TimerListener {
    
    JLabel timerLabel = new JLabel(generateTimeString(0));
    
    Timer timer = new Timer();
    
    public TimerPanel() {
    	setLayout(new BorderLayout());
    	
    	setBackground(Color.BLACK);
    	
    	timerLabel.setHorizontalAlignment(JLabel.CENTER);
        timerLabel.setFont(new Font("Courier New", Font.BOLD, 30));
        timerLabel.setForeground(Color.GREEN);
        timerLabel.setBorder(new EmptyBorder(5, 5, 5, 5));
        
        add(timerLabel, BorderLayout.CENTER);
        
        Thread timerThread = new Thread(timer, "TimerThread");
        timerThread.start();
        
        timer.addTimerListener(this);
    }
    
    public void timerEvent(TimerEvent e) {
    	timerLabel.setText(generateTimeString(e.getSecs()));
    }
    
    public Timer getTimer() {
        return timer;
    }
    
    public JLabel getTimerLabel() {
        return timerLabel;
    }
    
    public static String generateTimeString(int secs) {
    	String ret = "";
    	
    	int min = secs / 60;
        int sec = secs % 60;
        ret = (min < 10 ? "0" + min : min) + ":" + (sec < 10 ? "0" + sec : sec);
    	
    	return ret;
    }  
}
